package scau.erps.view.dashboard;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import scau.erps.app.Erps;
import scau.erps.service.OrderSetService;
import scau.erps.service.impl.OrderSetServiceImpl;

/**
 * 平均价格预测折线图
 * @author DCJ
 * 
 */
public class PriceChart{	
	/**
	 * 得到图片
	 * @return
	 */
	public static JFreeChart getChart(){
		JFreeChart chart = createChart(createDataset());
		return chart;
	}
	/**
	 * 数据
	 */
	public static DefaultCategoryDataset createDataset() {
		DefaultCategoryDataset linedataset = new DefaultCategoryDataset();
		// 各曲线名称
		String series1 = "Beryl";
		String series2 = "Crystal";
		String series3 = "Ruby";
		String series4 = "Saphire";

		OrderSetService orderSetService=Erps.getIocInstance().get(OrderSetServiceImpl.class);		
		String year = null;
		for (int i = 1; i < 8; i++) {
			year = "Y"+i;
			double price1 = orderSetService.getAveragePriceByProductAndYear(series1, year);
			linedataset.addValue(price1, series1, year);			
		}
		for (int i = 1; i < 8; i++) {
			year = "Y"+i;
			double price2 = orderSetService.getAveragePriceByProductAndYear(series2, year);
			linedataset.addValue(price2, series2, year);
		}
		for (int i = 1; i < 8; i++) {
			year = "Y"+i;
			double price3 = orderSetService.getAveragePriceByProductAndYear(series3, year);
			linedataset.addValue(price3, series3, year);
			
		}
		for (int i = 1; i < 8; i++) {
			year = "Y"+i;
			double price4 = orderSetService.getAveragePriceByProductAndYear(series4, year);
			linedataset.addValue(price4, series4, year);
			
		}
		return linedataset;
	}
	/**
	 * 绘制折线
	 */
	public static JFreeChart createChart(DefaultCategoryDataset linedataset) {
		  //createLineChart 参数依次为：标题，横坐标标题，纵坐标标题，数据，方向，图例，工具，超链接
		  JFreeChart chart = ChartFactory.createLineChart("产品价格-年份市场需求预测", "年份", "平均价格(M)", linedataset, PlotOrientation.VERTICAL, true, true, false);
		  return chart;
		 }
	}
